// pages/correctbook/correctbook.js

Page({

    /**
     * 页面的初始数据
     */
    data: {
        problemList: []
    },

    onLoad: function (options) {
    },

    onShow: async function () {
        this.getTabBar().setData({
            selected: 1
        });
        let app = getApp();
        wx.showLoading();
        let questions = await app.getWrongBook();
        wx.hideLoading();
        this.setData({
            problemList: questions
        });
    },

    tapItem: async function (e) {
        let pIndex = parseInt(e.currentTarget.dataset.index);
        wx.showActionSheet({
            itemList: ['复习这道题目', '删除'],
            success: async res => {
                let app = getApp();
                let tapIndex = res.tapIndex;

                switch(tapIndex) {
                    case 0:
                        app.installQuestionSet(app.globalData.wrongBook);
                        app.globalData.problemIndex = pIndex;
                        wx.navigateTo({
                            url: "/pages/problem/problem"
                        });
                        break;
                    case 1:
                        wx.showLoading();
                        await app.deleteQuestion(this.data.problemList[pIndex].id);
                        let questions = await app.getWrongBook();
                        wx.hideLoading();
                        this.setData({
                            problemList: questions
                        })
                        break;
                }
            }
        })
    }
})